package roborally.commandCombiners;
import roborally.*;
import roborally.Statement;
import roborally.Conditions.*;
import roborally.Commands.*;
import roborally.conditionCombiners.ConditionCombiner;

/**
 * A class of if-commands for playing the roborally game, involving a condition, a command to be executed if the condition holds (thenCommand)
 * and a command to be executed if the condition does not hold (elseCommand).
 * 
 * @version  2.90
 * @author   Michiel Ruelens & Dieter Naten
 */
public class If extends CommandCombiner {
	
	private final Statement condition;
	private final Command thenCommand;
	private final Command elseCommand;
	
	/**
	 * Initialise the new if-command with the given parameters.
	 * @param 	condition
	 * 			The condition that is being validated.
	 * @param 	thenCommand
	 * 			The command that is executed when the condition is true.
	 * @param 	elseCommand
	 * 			The command that is executed when the condition is false.
	 * 			
	 */
	public If(Statement condition, Command thenCommand, Command elseCommand){
		assert(condition instanceof Condition || condition instanceof ConditionCombiner);
		this.condition = condition;
		this.thenCommand = thenCommand;
		this.elseCommand = elseCommand;
		this.setStringForm("if ("+condition.getStringForm()+") then ("+thenCommand.getStringForm()+") else ("+elseCommand.getStringForm()+")");
	}
	
	public Statement getCondition() {
		return condition;
	}

	public Command getThenCommand() {
		return thenCommand;
	}

	public Command getElseCommand() {
		return elseCommand;
	}

	/**
	 * Execute this if statement for the given robot. If the condition of this if-command is true then the thenCommand is executed,
	 * otherwise the elseCommand is executed.
	 * @param	robot
	 * 			The robot that wants to execute this command.
	 */
	public void execute(Robot robot){
		if(condition.validate(robot)){
			thenCommand.execute(robot);
		}
		else{
			elseCommand.execute(robot);
		}
	}

}
